package com.dotflat.fin.dto;

import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Map;

@Data
public class AppPayResult {

//	@Column(columnDefinition = "INT DEFAULT 0 COMMENT '支付途径， 0.wechat   1.alipay'")
	private Integer gateway;

	private String outTradeNo;

	private String appId;

	private String mchId;

	private String openId;

	private String transactionId;

	private String resultCode;

	private String tradeType;

	private String bankType;

	private Integer totalFee;

	private String attach;

	private String timeEnd;

	public static AppPayResult of(AliPayNotifyParam notifyParam){
		AppPayResult result = new AppPayResult();

		result.setOutTradeNo(notifyParam.getOutTradeNo());
		result.setAppId(notifyParam.getAppId());
		result.setMchId(notifyParam.getSellerId());
		result.setOpenId(notifyParam.getBuyerId());

		result.setTransactionId(notifyParam.getTradeNo());
		result.setResultCode(notifyParam.getTradeStatus());
//		result.setTradeType(notifyParam.getTradeType());
//		result.setBankType(notifyParam.getBankType());
		result.setTimeEnd(notifyParam.getGmtCreate());

		BigDecimal receiptAmount = new BigDecimal(notifyParam.getReceiptAmount());
		BigDecimal temp = receiptAmount.multiply(new BigDecimal("100"));  //圆变成分
		int totalFee = 100 * temp.intValue();
		result.setTotalFee(totalFee);
		result.setAttach(notifyParam.getPassbackParams());

		return result;
	}

	public static AppPayResult of(WxPayOrderQueryResult queryResult){
		AppPayResult result = new AppPayResult();

		result.setOutTradeNo(queryResult.getOutTradeNo());
		result.setAppId(queryResult.getAppid());
		result.setMchId(queryResult.getMchId());
		result.setOpenId(queryResult.getOpenid());

		result.setTransactionId(queryResult.getTransactionId());
		result.setResultCode(queryResult.getResultCode());
		result.setTradeType(queryResult.getTradeType());
		result.setBankType(queryResult.getBankType());
		result.setTimeEnd(queryResult.getTimeEnd());

		result.setTotalFee(queryResult.getTotalFee());
		result.setAttach(queryResult.getAttach());

		return result;
	}

	public static AppPayResult of(Map<String, String> notifyMap){
		AppPayResult result = new AppPayResult();

		String resultCode = (String) notifyMap.get("result_code");
		String transactionId = (String) notifyMap.get("transaction_id");
		String outTradeNo = (String) notifyMap.get("out_trade_no");
		String mchId = (String) notifyMap.get("mch_id");
		String bankType = (String) notifyMap.get("bank_type");
		String timeEnd = (String) notifyMap.get("time_end");
		String attach = (String) notifyMap.get("attach");
		Integer totalFee = Integer.parseInt(notifyMap.get("total_fee"));

		result.setOutTradeNo(outTradeNo);
		result.setTransactionId(transactionId);
		result.setMchId(mchId);
		result.setBankType(bankType);
		result.setTimeEnd(timeEnd);
		result.setResultCode(resultCode);
		result.setAttach(attach);
		result.setTotalFee(totalFee);

		return result;
	}
}
